<!--
 * @Description: 缺陷分类列表
 * @Author: Liu Zhiwang
 * @Date: 2021-04-13 16:27:07
 * @LastEditTime: 2021-07-01 12:43:48
 * @LastEditors: Liu Zhiwang
-->
<template>
  <div class="app-container v-flex">
    <div ref="main" class="v-flex-elastic">
      <base-table
        :height="height"
        :loading="listLoading"
        :data="tableData"
        :columns="columns"
        show-filter-column
        operaton-width="150px"
        @updateForm="updateForm" />
    </div>

    <!-- 新增或编辑弹框 -->
    <DataAddOrEdit :dialog="dialog" @updateData="updateData" />
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { tableMixin } from '@/utils/public'
import DataAddOrEdit from './DataAddOrEdit'
import { getListByTypeCode, updateOrEditData } from '@/api/jcwwtk/categoryRectLevel'
export default {
  name: 'JCWWTKClassify',
  components: { DataAddOrEdit },
  mixins: [tableMixin],
  data() {
    return {
      listQuery: {
        typeCode: 'JCJCTYPE'
      },
      total: 0,
      height: 0,
      listLoading: false,
      tableData: [],
      columns: [
        // { label: '所属单位', prop: 'shortName' },
        { label: 'C类型名称', prop: 'itemName' }
      ],
      filterData: {
      },
      dialog: {
        title: '',
        visible: false,
        disabled: false,
        loading: false,
        form: {
          dictionaryId: null,
          typeList: [],
          itemName: null
        },
        initForm: {
          status: 1,
          delFlag: 0,
          parentId: 0,
          parentName: '',
          deptCode: ''
        },
        dataList: {
          categoryList: [], // 分类列表
          levelList: [] // 等级列表
        },
        rules: {
        }
      },
      dialogLook: {
        visible: false
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  mounted() {
    this.listQuery.deptCode = this.userInfo.unitDeptCode
  },
  created() {
    this.getListPage()
  },
  methods: {

    // 编辑
    updateForm(row, index) {
      this.currentRow = [row]
      const dialog = {
        title: '关联配置编辑',
        type: 'edit',
        visible: true,
        disabled: false,
        loading: false,
        form: JSON.parse(JSON.stringify(row))
      }
      this.dialog = Object.assign(this.dialog, dialog)
    },
    // 查询
    search() {
      this.getListPage()
    },
    // 列表查询
    getListPage() {
      this.listLoading = true
      getListByTypeCode(this.listQuery)
        .then((response) => {
          this.tableData = response.data.JCJCTYPE
          this.listLoading = false
        })
        .catch((_) => {
          this.listLoading = false
        })
    },
    // 新增或编辑弹框$emit 回调方法
    updateData(formData) {
      updateOrEditData(formData.typeList)
        .then((response) => {
          this.currentRow = [response.data]
          if (this.dialog.type === 'add') {
            this.tableData.unshift(this.currentRow[0])
            this.total++
            this.notifiySuccess('添加成功')
            var dialog = this.dialog
            dialog.form.id = response.data.id
            this.dialog = Object.assign(this.dialog, dialog)
          } else {
            // const index = this.tableData.findIndex(
            //   (item) => item.id === this.currentRow[0].id
            // )
            // this.tableData.splice(
            //   index,
            //   1,
            //   JSON.parse(JSON.stringify(this.currentRow[0]))
            // )
            // this.$forceUpdate()
            this.notifiySuccess('保存成功')
            this.getListPage()
          }
          this.dialog.loading = false
          this.dialog.visible = false
        })
        .catch((_) => {
          this.dialog.loading = false
        })
    }
  }
}
</script>

